package edu.fiu.cis.personalize.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.fiu.cis.personalize.shared.GoogleDocument;
import edu.fiu.cis.personalize.shared.User;

public class ResultDisplay extends Composite {

	public ResultDisplay(GoogleDocument document, User user,
			PersonalizedSearchServiceAsync service, int qid) {
		Anchor anchor = new Anchor(document.getTitle(), document.getURL());
		anchor.setTarget("blank");
		anchor.addClickHandler(new AnchorClickHandler(user.getId(), document
				.getId(), qid, service));

		HorizontalPanel hp = new HorizontalPanel();
		hp.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);

		HTML stats = new HTML(
				"<font size=\"small\" color=\"red\">object rank: "
						+ document.getObjectRank() + " &nbsp;(Google : "
						+ document.getGoogleRank() + ")");
		hp.add(anchor);
		hp.setCellWidth(anchor, "650px");

		hp.add(stats);
		HTML html = new HTML(document.getContent());

		VerticalPanel panel = new VerticalPanel();
		panel.add(hp);
		panel.add(html);

		panel.addStyleName("result");
		initWidget(panel);

		anchor.addStyleName("result-title");

		hp.setWidth("750px");

		stats.addStyleName("result-stats");
		stats.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);

	}

	class AnchorClickHandler implements ClickHandler {
		private int userId;
		private int documentId;
		private int queryId;
		private PersonalizedSearchServiceAsync searchService;

		public AnchorClickHandler(int userId, int documentId, int queryId,
				PersonalizedSearchServiceAsync searchService) {
			super();
			this.userId = userId;
			this.documentId = documentId;
			this.queryId = queryId;
			this.searchService = searchService;
		}

		@Override
		public void onClick(ClickEvent event) {
			searchService.clickGoogleDocument(userId, queryId, documentId,
					new VoidCallback());
		}

	}
}
